package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.AskForLeaveBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class AskForLeaveAdapter extends RecyclerView.Adapter<AskForLeaveAdapter.MyHolder> {

    private Context mContext;
    public List<AskForLeaveBean.DataBean> list = null;
    private OnLister lister;

    public AskForLeaveAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_ask_for_leave, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        AskForLeaveBean.DataBean bean = list.get(position);

        Glide.with(mContext).load(bean.getAvatar())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext))
                .into(viewHolder.imgIcon);

        viewHolder.tvName.setText(bean.getTeacherName());
        viewHolder.tvTime.setText(bean.getSubmitTime());

        if (bean.getCause().length() > 200) {
            String content = bean.getCause().substring(0, 197);
            viewHolder.tvContent.setText(content + "...");
        } else {
            viewHolder.tvContent.setText(bean.getCause());
        }

        viewHolder.tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lister != null) {
                    lister.lister(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<AskForLeaveBean.DataBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 加载添加数据
     *
     * @param list
     */
    public void addDataAll(List<AskForLeaveBean.DataBean> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<AskForLeaveBean.DataBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgIcon;
        public TextView tvName;
        public TextView tvTime;
        public TextView tvContent;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgIcon = itemView.findViewById(R.id.img_icon);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }

    public interface OnLister {
        void lister(int position);
    }
}